package ext.tianma.bom.workflow;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import wt.fc.ObjectReference;
import wt.fc.WTObject;
import wt.fc.collections.WTArrayList;
import wt.lifecycle.LifeCycleState;
import wt.part.WTPart;
import wt.util.WTException;
import ext.com.core.CoreUtil;

public class CusReWriteBOMreviewConfirmBusiness {
	
	//新品状态
	private static final String NEW_PRODUCT = "NewProduct" ;
	
	//量产发布
	private static final String MASS_PRODUCTION_RELEASE = "MassProductionRelease" ;
	
	//批准
	private static final String APPROVAL = "APPROVAL" ;
	
	private static final String INWORK = "INWORK" ;
	
	private static List<String> list = new ArrayList<String>();
	
	static{
		list.add(INWORK);
		list.add(APPROVAL);
		list.add(NEW_PRODUCT);
		list.add(MASS_PRODUCTION_RELEASE);
	}
	
	/**
	 * 初始化随迁列表及不符合列表
	 * @param pbo
	 * @param reviewObjs
	 * @param inconformityReviewObjs
	 */
	public static void showPageData( Object pbo , WTArrayList reviewObjs , WTArrayList inconformityReviewObjs ){
		
		if( pbo == null || !(pbo instanceof WTPart) ){
			return ;
		}

		WTPart part = ( WTPart )pbo;
		
		if( part != null ){
			List< WTPart > partList = ReWriteBOMReviewConfirmBusiness.getAllSubPartsWithSubstitute( part );
			
			System.out.println( "Debug   partList : " + partList.size() );
			
			if( partList != null && partList.size() > 0 ){
				Iterator< WTPart > iterator = partList.iterator();
				
				while( iterator.hasNext() ){
					WTPart subPart = iterator.next();
					
					if( subPart != null ){
						
						LifeCycleState state = subPart.getState();
						
						if( state != null ){
							String partState = state.toString();
							System.out.println( "Debug   showPageData...partState : " + partState );	
							if( partState.equals( INWORK ) ){
								
								if( checkListContainsPart( subPart , reviewObjs)){
									reviewObjs.add( subPart );
									
									ReWriteBOMReviewConfirmBusiness.checkPartDesignSate( subPart , reviewObjs , inconformityReviewObjs );
								}	
								
								if( !checkListContainsPart( subPart , inconformityReviewObjs ) ){
									inconformityReviewObjs.remove( subPart );
								}
							}else if( !list.contains( partState ) ){
								
								if( checkListContainsPart( subPart , inconformityReviewObjs ) && checkListContainsPart( subPart , reviewObjs ) ){
									inconformityReviewObjs.add( subPart );
								}							
							}					
						}
					}
				}
			}
		}		
	}
	
	/**
	 * 检查集合中是否存在该对象
	 * 
	 * 包含返回：false
	 * @param part
	 * @param arrayList
	 * @return
	 */
	public static boolean checkListContainsPart( WTPart part , WTArrayList arrayList ){
		boolean checkListConnectPart = true;
		
		if( part == null || arrayList == null || arrayList.size() == 0 ){
			return checkListConnectPart;
		}
		
		for( int i = 0 ; i < arrayList.size() ; i++ ){
			Object object = arrayList.get(i);
			
			if( object != null ){
				
				if( object instanceof ObjectReference ){
					ObjectReference orf = ( ObjectReference )object;
					object = orf.getObject();
				}
				
				if( object instanceof WTPart ){
					WTPart arrayPart = ( WTPart )object;
					
					try {
						part = CoreUtil.getWTPartByNumberAndView( part.getNumber() , part.getViewName() );
						
						arrayPart = CoreUtil.getWTPartByNumberAndView( arrayPart.getNumber() , arrayPart.getViewName() );
						
						if( arrayPart.equals( part ) ){
							checkListConnectPart = false;
						}
					} catch (WTException e) {
						e.printStackTrace();
					}
				}				
			}			
		}	
		return checkListConnectPart;		
	}
	
	/**
	 * 过滤数据
	 * @param wtObject
	 * @param wtList
	 * @return
	 */
	public static WTArrayList filterSubPart( WTObject wtObject , WTArrayList wtList ){
		WTArrayList list = new WTArrayList();
		if( wtList == null || wtObject == null || !( wtObject instanceof WTPart ) ){
			return list;
		}
		
		WTPart part = ( WTPart )wtObject;
		
		String number = part.getNumber();
		
		if( part != null ){
			//获取零部件下所有的子件
			List< WTPart > partList = ReWriteBOMReviewConfirmBusiness.getAllSubPartsWithSubstitute( part );
			
			if( partList != null && partList.size() > 0 ){
				
				for( int i = 0 ; i < wtList.size() ; i++ ){
					Object object = wtList.get(i);
					
					if( object != null ){
						
						if( object instanceof ObjectReference ){
							ObjectReference obf = ( ObjectReference )object;
							object = obf.getObject();
						}
						
						if( object instanceof WTPart ){
							WTPart subPart = ( WTPart )object;
							
							String subNumber = subPart.getNumber();
							
							Iterator< WTPart > iterator = partList.iterator();
							while( iterator.hasNext() ){
								WTPart newPart = iterator.next();
								
								if( newPart != null ){
									String newNumber = newPart.getNumber();
									//随迁列表中零部件存在与零部件的子件中添加到集合中
									if( subNumber.equals( newNumber ) ){
										list.add( subPart );
									}else if( subNumber.equals( number ) ){
										list.add( subPart );
									}
								}
							}
						}
					}
				}
			}			
		}		
		return list;		
	}
}
